﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using DAL;
using HomeStayWebCP.Models.BusinessObject;

namespace HomeStayWebCP.Models
{
    public class SearchPostModel
    {
        public List<ResultObject> ListResultSearch { get; set; }
        public List<ResultObject> ListResultSearchText { get; set; } 
        public List<ResultObject> SearchPost(SearchPostBO DataSearchDTO)
        {
            SearchPostDataModel search = new SearchPostDataModel();

            SearchObject DataSearch = new SearchObject()
            {
                ProvinceID = DataSearchDTO.ProvinceID,
                DistrictID = DataSearchDTO.DistrictID,
                Housetype = DataSearchDTO.Housetype,
                Pricetype = DataSearchDTO.Pricetype,
                MinPrice = DataSearchDTO.MinPrice,
                MaxPrice = DataSearchDTO.MaxPrice,
                MinArea = DataSearchDTO.MinArea,
                MaxArea = DataSearchDTO.MaxArea,
                HasGara = DataSearchDTO.HasGara,
                HasGarden = DataSearchDTO.HasGarden,
                HasKitchen = DataSearchDTO.HasKitchen,
                HomestaytypeID = DataSearchDTO.HomestaytypeID

            };
            List<Post_ResultSearch_View> ListSearchResult = search.SearchPost(DataSearch);
            var query0 = from result in ListSearchResult
                         select result.PostID;
            List<int> list = query0.Distinct().ToList();
            List<Post_ResultSearch_View> lastresult = new List<Post_ResultSearch_View>();
            if (DataSearchDTO.HomestaytypeID != null)
            {
                foreach (var i in list)
                {

                    if (ListSearchResult.Where(s => s.PostID == i).ToList().Count == DataSearchDTO.HomestaytypeID.Count())
                    {
                        lastresult.Add(ListSearchResult.Where(s => s.PostID == i).First());
                    }

                }
            }
            else
            {
                foreach (var i in list)
                {
                    lastresult.Add(ListSearchResult.Where(s => s.PostID == i).First());


                }
            }
            int countrow = 1;
            //return last result add row for paging
            var query1 = from item in lastresult
                         orderby item.PostID descending 
                         select new 
                                    {
                                        item.PostID,
                                        item.Title,
                                        item.Province,
                                        item.District,
                                        item.AvatarPost,
                                        item.AvatarUser,
                                        item.PricePerDay,
                                        item.PricePerWeek,
                                        item.PricePerMonth,
                                        item.AddressHouse,
                                        item.HomeStayTypeName,
                                        item.like_count,
                                        item.Username,
                                        item.ProvinceID,
                                        item.DistrictID,
                                        item.IsAvailable,
                                        item.TimeCreated,
                                        row=countrow++//add row 

                                    };
            var rowresult = query1.ToList();
            List<ResultObject> listresult=new List<ResultObject>();
            foreach (var item in rowresult)
            {
               ResultObject obj=new ResultObject();
                obj.PostID = item.PostID;
                obj.Title = item.Title;
                obj.Province = item.Province;
                obj.District = item.District;
                obj.AvatarPost=item.AvatarPost;
                obj.AvatarUser=item.AvatarUser;
                obj.PricePerDay = item.PricePerDay;
                obj.PricePerWeek = item.PricePerWeek;
                obj.PricePerMonth = item.PricePerMonth;
                obj.AddressHouse=item.AddressHouse;                
                obj.HomeStayTypeName = item.HomeStayTypeName;
                obj.like_count = item.like_count;
                obj.IsAvailable = item.IsAvailable;
                obj.TimeCreated = item.TimeCreated;
                obj.Username = item.Username;
                obj.Row = item.row;
                listresult.Add(obj);
            }
            ListResultSearch = listresult;
            return listresult;
        }
        public List<ResultObject> SearchText(String txtSearch)
        {
            SearchPostDataModel search = new SearchPostDataModel();
            List<Post_ResultSearch_View> listSearchTextResult = search.SearchText(txtSearch);
            var query1 = from result in listSearchTextResult
                         select result.PostID;
            List<int> list = query1.Distinct().ToList();
            List<Post_ResultSearch_View> lastresult = new List<Post_ResultSearch_View>();

            foreach (var i in list)
            {
                lastresult.Add(listSearchTextResult.Where(s => s.PostID == i).First());
            }
            int countrow = 1;
            //return last result add row for paging
            var query2 = from item in lastresult
                         orderby item.PostID descending 
                         select new
                         {
                             item.PostID,
                             item.Title,
                             item.Province,
                             item.District,
                             item.AvatarPost,
                             item.AvatarUser,
                             item.PricePerDay,
                             item.AddressHouse,
                             item.HomeStayTypeName,
                             item.like_count,
                             item.Username,
                             item.PricePerWeek,
                             item.PricePerMonth,
                             item.IsAvailable,
                             item.TimeCreated,
                             row = countrow++//add row 

                         };
            var rowresult = query2.ToList();
            List<ResultObject> listresult = new List<ResultObject>();
            foreach (var item in rowresult)
            {
                ResultObject obj = new ResultObject();
                obj.PostID = item.PostID;
                obj.Title = item.Title;
                obj.Province = item.Province;
                obj.District = item.District;
                obj.AvatarPost = item.AvatarPost;
                obj.AvatarUser = item.AvatarUser;
                obj.PricePerDay = item.PricePerDay;
                obj.AddressHouse = item.AddressHouse;
                obj.HomeStayTypeName = item.HomeStayTypeName;
                obj.like_count = item.like_count;
                obj.PricePerDay = item.PricePerDay;
                obj.PricePerWeek = item.PricePerWeek;
                obj.PricePerMonth = item.PricePerMonth;
                obj.IsAvailable = item.IsAvailable;
                obj.TimeCreated = item.TimeCreated;
                obj.Row = item.row;
                listresult.Add(obj);
            }
            ListResultSearchText = listresult;

            return listresult;
        }

        private int pageNumber;
        private int countNo;

        private int CheckPage(String page)
        {
            int curpage = 1;
            if (page != null)
            {
                if (Regex.IsMatch(page, "[0-9]+"))
                {
                    curpage = int.Parse(page.Trim());
                    if (curpage > countNo)
                    {
                        curpage = countNo;
                    }
                    if (curpage < 1)
                    {
                        curpage = 1;
                    }
                }
            }
            return curpage;
        }


        public int SetCountPaging(int rownumber, SearchPostBO DataSearchDTO)
        {
            pageNumber = rownumber;
            int countrows;
            if(ListResultSearch!=null)
            {
                countrows = ListResultSearch.Count;//count how many rows of result
            }
            else
            {
                ListResultSearch = SearchPost(DataSearchDTO);
                countrows = ListResultSearch.Count;
            }
           
            
            countNo = countrows % pageNumber == 0 ? countrows / pageNumber : countrows / pageNumber + 1;
            return countNo;
        }

        public int SetCountPagingText(int rownumber, string txtSearch)
        {
            pageNumber = rownumber;
            int countrows;
            if (ListResultSearchText!=null)
            {
                countrows = ListResultSearchText.Count;
            }
            else
            {
                ListResultSearchText = SearchText(txtSearch);
                countrows = ListResultSearchText.Count;
            }
            countNo = countrows%pageNumber == 0 ? countrows/pageNumber : countrows/pageNumber + 1;
            return countNo;
        }

        public List<ResultObject> GetResultSearchtOfPaing(ref String pageindex)
        {
            List<ResultObject> listresultPaging = new List<ResultObject>();
           int page = CheckPage(pageindex);//kiểm tra trang đúng cú pháp chưa
            pageindex = page.ToString(CultureInfo.InvariantCulture);//set lại giá trị sau khi kiểm tra
            long? start = page * pageNumber - pageNumber;
            long? end = page * pageNumber;

            listresultPaging = ListResultSearch.Where(o => o.Row > start && o.Row <= end).ToList();

            return listresultPaging;
        }
        
        public List<ResultObject> GetResultSearchtTextOfPaing (ref String pageIndex)
        {
            List<ResultObject> listresultPaging = new List<ResultObject>();
            int page = CheckPage(pageIndex);
            pageIndex = page.ToString(CultureInfo.InvariantCulture);
            long? start = page * pageNumber - pageNumber;
            long? end = page*pageNumber;
            listresultPaging = ListResultSearchText.Where(o => o.Row > start && o.Row <= end).ToList();
            return listresultPaging;
        }

        public ProvinceDistrictID GetProDisId (string txtSearch)
        {
            SearchPostDataModel dataModel = new SearchPostDataModel();
            var lisID = dataModel.GetProDisId(txtSearch).ToList();
            ProvinceDistrictID obj = new ProvinceDistrictID();
            obj.ProvinceID = lisID[0].ProvinceId;
            obj.DistrictID = lisID[0].DistrictId;
            return obj;
        } 
    }

}